<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateOrdersTable extends Migrator
{
    /**
     * 创建订单表
     */
    public function change()
    {
        $table = $this->table('orders', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_unicode_ci',
            'comment' => '订单表'
        ]);

        $table->addColumn('order_no', 'string', [
            'limit' => 100,
            'null' => false,
            'comment' => '订单号'
        ])
        ->addColumn('platform', 'string', [
            'limit' => 50,
            'null' => false,
            'default' => 'xiaofeng',
            'comment' => '平台'
        ])
        ->addColumn('order_date', 'date', [
            'null' => false,
            'comment' => '订单日期'
        ])
        ->addColumn('customer_name', 'string', [
            'limit' => 100,
            'null' => true,
            'comment' => '客户姓名'
        ])
        ->addColumn('product_name', 'string', [
            'limit' => 200,
            'null' => true,
            'comment' => '商品名称'
        ])
        ->addColumn('quantity', 'integer', [
            'null' => false,
            'default' => 1,
            'comment' => '数量'
        ])
        ->addColumn('unit_price', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '单价'
        ])
        ->addColumn('total_amount', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '总金额'
        ])
        ->addColumn('cost_price', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '成本价'
        ])
        ->addColumn('profit', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '利润'
        ])
        ->addColumn('status', 'string', [
            'limit' => 50,
            'null' => false,
            'default' => 'pending',
            'comment' => '订单状态'
        ])
        ->addColumn('source_data', 'text', [
            'null' => true,
            'comment' => '原始数据'
        ])
        ->addColumn('sync_date', 'datetime', [
            'null' => true,
            'comment' => '同步时间'
        ])
        ->addColumn('created_at', 'datetime', [
            'null' => false,
            'comment' => '创建时间'
        ])
        ->addColumn('updated_at', 'datetime', [
            'null' => false,
            'comment' => '更新时间'
        ]);

        $table->addIndex(['order_no'], ['name' => 'idx_order_no', 'unique' => true]);
        $table->addIndex(['platform'], ['name' => 'idx_platform']);
        $table->addIndex(['order_date'], ['name' => 'idx_order_date']);
        $table->addIndex(['status'], ['name' => 'idx_status']);
        $table->addIndex(['sync_date'], ['name' => 'idx_sync_date']);

        $table->create();
    }
} 